package com.cch.demo.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cch.demo.sys.domain.Loginfo;
import com.cch.demo.sys.domain.Notice;
import com.cch.demo.sys.domain.User;
import com.cch.demo.sys.service.NoticeService;
import com.cch.demo.sys.util.DataGridView;
import com.cch.demo.sys.util.ResultObj;
import com.cch.demo.sys.util.WebUtils;
import com.cch.demo.sys.vo.LogInfoVo;
import com.cch.demo.sys.vo.NoticeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 华哥
 * @since 2020-03-07
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Resource
    private NoticeService noticeServiceImpl;

    @RequestMapping("/loadAllNotice")
    public DataGridView loadAllNotice(NoticeVo noticeVo){
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(noticeVo.getTitle()),"title",noticeVo.getTitle());
        queryWrapper.like(StringUtils.isNotBlank(noticeVo.getOpername()),"opername",noticeVo.getOpername());
        queryWrapper.le(noticeVo.getEndTime()!=null,"createtime",noticeVo.getEndTime());
        queryWrapper.ge(noticeVo.getStartTime()!=null,"createtime",noticeVo.getStartTime());
        queryWrapper.orderByDesc("createtime");
        IPage<Notice> page = new Page<>(noticeVo.getPage(),noticeVo.getLimit());
        noticeServiceImpl.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @RequestMapping("addNotice")
    public ResultObj addNotice(NoticeVo noticeVo){
        try{
            noticeVo.setCreatetime(new Date());
            User user = (User)WebUtils.getSession().getAttribute("user");
            noticeVo.setOpername(user.getName());
            noticeServiceImpl.save(noticeVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    @RequestMapping("updateNotice")
    public ResultObj updateNotice(NoticeVo noticeVo){
        try{
            QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id",noticeVo.getId());

            noticeVo.setCreatetime(new Date());
            User user = (User)WebUtils.getSession().getAttribute("user");
            noticeVo.setOpername(user.getName());
            noticeServiceImpl.update(noticeVo,queryWrapper);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    @RequestMapping("deleteNotice")
    public ResultObj deleteNotice(Integer id){
        try{
            noticeServiceImpl.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    @RequestMapping("batchDeleteNotice")
    public ResultObj BatchDeleteNotice(NoticeVo noticeVo){
        try{
            List<Serializable> idlist = new ArrayList<>();
            for (Integer id:noticeVo.getIds()){
                idlist.add(id);
            }
            noticeServiceImpl.removeByIds(idlist);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

}

